package com.test.daily.leetcode.y2022.m07.day0701.v06;

/**
 * @author Tom on 2022/7/3
 */
public class Solution {
    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }

        if(i >= 0){
            int j = nums.length - 1;
            while(j >= 0 && nums[i]>= nums[j]){
                j--;
            }
            swap(nums, i, j);
        }

        reverse(nums, i+ 1);
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = arr[i];
    }

    public static void reverse(int[] arr, int start) {
        int left = start, right = arr.length - 1;
        while (left < right) {
            swap(arr, left, right);
            left++;
            right--;
        }
    }
}
